package com.mall.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

/**
 * 短信验证码发送工具类
 *
 * @author xiao-chen
 */
@Slf4j
public class SmsUtil {


    private static final String SIGNNAME = "cxx";

    private static final String TEMPLATECODE = "SMS_275460198";

    /**
     * 发送短信
     *
     * @param phoneNumber 手机号
     * @param param       验证码
     */
    public static void sendMessage(String phoneNumber, String param) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tRTnLzTkxBi2rT2rhki", "GAg5ehWWy7ANU7fYuqm5LkmSJ8m18k");
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSysRegionId("cn-hangzhou");
        //接收短信的手机号码
        request.setPhoneNumbers(phoneNumber);
        //短信签名名称
        request.setSignName(SIGNNAME);
        //短信模板CODE
        request.setTemplateCode(TEMPLATECODE);
        //短信模板变量对应的实际值
        request.setTemplateParam("{\"code\":\"" + param + "\"}");
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            log.info("短信发送成功!");
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

}
